package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.MesEquipment;
import com.ruoyi.system.domain.WmsMaterialInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MesFactoryModel;
import com.ruoyi.system.service.IMesFactoryModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工厂建模Controller
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/system/model")
public class MesFactoryModelController extends BaseController
{
    @Autowired
    private IMesFactoryModelService mesFactoryModelService;

    /**
     * 查询工厂建模列表
     */
    @GetMapping("/wl")
    public List<MesEquipment> wl()
    {
      return mesFactoryModelService.wl();
    }
    @PostMapping("/selewl")
    public  List<WmsMaterialInfo> selewl(@RequestBody WmsMaterialInfo wmsMaterialInfo){
        System.out.println(wmsMaterialInfo);
        List<WmsMaterialInfo> w=mesFactoryModelService.findwl(wmsMaterialInfo.getCode());
        List <WmsMaterialInfo> wuliao=new ArrayList<>();
        List<WmsMaterialInfo> sj;
        for (int i = 0; i < w.size(); i++) {
            sj=mesFactoryModelService.selewl(null,null,null,null,w.get(i).getCode());
            System.out.println(sj);
            wuliao.addAll(sj);
        }
        return wuliao;
    }
    @PostMapping("/seleid")
    public List<WmsMaterialInfo> seleid(@RequestBody WmsMaterialInfo wmsMaterialInfo){
        if(wmsMaterialInfo.getId() !=null){
            return mesFactoryModelService.selewl(Math.toIntExact(wmsMaterialInfo.getId()),null,null,null,null);
        }
       return  null;
    }
    @PostMapping("up")
    public  List<WmsMaterialInfo> up (@RequestBody WmsMaterialInfo wmsMaterialInfo){
        return mesFactoryModelService.selewl(Math.toIntExact(wmsMaterialInfo.getId()),null,null,null,null);
    }
    @PreAuthorize("@ss.hasPermi('system:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesFactoryModel mesFactoryModel)
    {
        startPage();
        List<MesFactoryModel> list = mesFactoryModelService.selectMesFactoryModelList(mesFactoryModel);
        return getDataTable(list);
    }
    /**
     * 导出工厂建模列表
     */
    @PreAuthorize("@ss.hasPermi('system:model:export')")
    @Log(title = "工厂建模", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesFactoryModel mesFactoryModel)
    {
        List<MesFactoryModel> list = mesFactoryModelService.selectMesFactoryModelList(mesFactoryModel);
        ExcelUtil<MesFactoryModel> util = new ExcelUtil<MesFactoryModel>(MesFactoryModel.class);
        util.exportExcel(response, list, "工厂建模数据");
    }

    /**
     * 获取工厂建模详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mesFactoryModelService.selectMesFactoryModelById(id));
    }

    /**
     * 新增工厂建模
     */
    @PreAuthorize("@ss.hasPermi('system:model:add')")
    @Log(title = "工厂建模", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesFactoryModel mesFactoryModel)
    {
        mesFactoryModelService.insert(mesFactoryModel);
        System.out.println(mesFactoryModel);
        for(int i=0;i<mesFactoryModel.getMesFactoryModelDetailList().size();i++){
            Long id = mesFactoryModel.getMesFactoryModelDetailList().get(i).getId();
            mesFactoryModelService.insertwl(mesFactoryModel.getId(),id);
        }
        return null;
    }

    /**
     * 修改工厂建模
     */
    @PreAuthorize("@ss.hasPermi('system:model:edit')")
    @Log(title = "工厂建模", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesFactoryModel mesFactoryModel)
    {
        return toAjax(mesFactoryModelService.updateMesFactoryModel(mesFactoryModel));
    }

    /**
     * 删除工厂建模
     */
    @PreAuthorize("@ss.hasPermi('system:model:remove')")
    @Log(title = "工厂建模", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesFactoryModelService.deleteMesFactoryModelByIds(ids));
    }
}
